﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;
using DevExpress.XtraEditors;
namespace BanHangCshap
{
    public static class ControlItemsStatic
    {
        public static void fillGridLookUpEdit(DevExpress.XtraEditors.GridLookUpEdit glku, DevExpress.XtraGrid.Views.Grid.GridView glkuv,DataTable dt, string valuemember, string displaymember, string[] fielddisplay)
        {
            
            glku.Properties.DataSource = dt;
            glku.Properties.ValueMember = valuemember;
            glku.Properties.DisplayMember = displaymember;
            for (int i = 0; i < glkuv.Columns.Count; i++)
            {
                glkuv.Columns[i].Visible = false;
            }
            for (int i = 0; i<glkuv.Columns.Count; i++)
            {
                for (int j = 0; j < fielddisplay.Length; j++)
                {
                    if (glkuv.Columns[i].FieldName == fielddisplay[j])
                    {
                        glkuv.Columns[i].Visible = true;
                        continue;
                    }
                }
            }

        }
        public static void fillListView(ListView lvw, DataTable dt, string[] headerText, int[] headerWidth)
        {

            int i = 0;
            lvw.Items.Clear();
            lvw.GridLines = true;
            for (i = 1; i <= headerText.Length; i++)
            {
                lvw.Columns.Add(headerText[i - 1], headerWidth[i - 1], 0);
            }
            if (dt.Rows.Count != 0)
            {
                ListViewItem lvi = null;
                i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    i = i + 1;
                    lvi = new ListViewItem(i.ToString());
                    foreach (object obj in dr.ItemArray)
                    {
                        lvi.SubItems.Add(Convert.ToString(obj));
                    }
                    lvw.Items.Add(lvi);
                }
            }
            lvw.View = View.Details;
        }
        public static void fillListBox(ListBox lst, DataTable dt, string valuemember, string displaymember)
        {
            lst.Items.Clear();


            if (dt != null)
            {
                lst.DataSource = dt;
                lst.DisplayMember = displaymember;
                lst.ValueMember = valuemember;
            }

        }
        public static void fillCombobox(System.Windows.Forms.ComboBox cbo, DataTable dt, string valuemember, string displaymember, int firstvalue, string firstdisplay)
        {
            cbo.Items.Clear();

            if (dt != null)
            {
                if (firstdisplay.Equals("") == false)
                {
                    DataRow dr = null;
                    dr = dt.NewRow();
                    dr[0] = firstvalue;
                    dr[1] = firstdisplay;
                    dt.Rows.InsertAt(dr, 0);
                }
                if (dt != null)
                {
                    cbo.DataSource = dt;
                    cbo.DisplayMember = displaymember;
                    cbo.ValueMember = valuemember;
                }
            }
        }
        //public void displayImage(string sql, PictureBox picbox)
        //{
        //    //Initialize image variable
        //    SqlCommand cmd = new SqlCommand();
        //    Image newImage = null;
        //    SqlDataReader dr = null;
        //    cmd = new SqlCommand();
        //    try
        //    {
        //        cmd.Connection = GetConnection();

        //        cmd.CommandText = sql;
        //        dr = cmd.ExecuteReader();
        //        //Neu ton tai mau tin
        //        if (dr.Read())
        //        {
        //            byte[] imageData = (byte)dr[0];
        //            //Read image data into a memory stream
        //            System.IO.MemoryStream ms = new System.IO.MemoryStream(imageData, 0, imageData.Length);
        //            ms.Write(imageData, 0, imageData.Length);
        //            //Set image variable value using memory stream.
        //            newImage = Image.FromStream(ms, true);
        //        }
        //        dr.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.ToString());
        //    }
        //    //set picture
        //    picbox.Image = newImage;
        //}


        public static void fillDataGridView(DataGridView dgv, DataTable dt, string[] headerText, int[] headerwidth)
        {


            dgv.Columns.Clear();
            int i = 0;
            dgv.DataSource = null;
            dgv.Columns.Add("", "");

            dgv.DataSource = dt;
            for (i = 0; i < headerText.Length; i++)
            {
                dgv.Columns[i].HeaderText = headerText[i];
                dgv.Columns[i].Width = headerwidth[i];

            }
            for (i = 0; i <= dgv.Rows.Count - 2; i++)
            {
                if (i % 2 == 0)
                {
                    dgv.Rows[i].DefaultCellStyle.BackColor = Color.GhostWhite;
                }
                dgv.Rows[i].Cells[0].Value = i + 1;

            }


        }
        //Hien thi du lieu len treeview
        public static void fillTreeViewOneLevel(TreeView trv, DataTable dt, ImageList iml)
        {


            trv.Nodes.Clear();
            trv.BeginUpdate();

            
            foreach (DataRow dr in dt.Rows)
            {
                //tn.Tag=dr[0].ToString();
                TreeNode tvNode = new TreeNode();

                tvNode.Tag = dr[0];
                tvNode.Text = Convert.ToString(dr[1]).Trim(' ');
                tvNode.ImageIndex = 0;
                tvNode.SelectedImageIndex = 1;
                //tn.Text = dr[1].ToString();
                trv.Nodes.Add(tvNode);



            }

            trv.EndUpdate();
            trv.ExpandAll();

        }
        public static void fillTreeViewTwoLevel(TreeView trv, DataTable dt1, DataTable dt2, ImageList iml)
        {

            trv.Nodes.Clear();
            trv.BeginUpdate();

            int i = 0;
            foreach (DataRow dr1 in dt1.Rows)
            {
                //tn.Tag=dr[0].ToString();
                TreeNode tvNode1 = new TreeNode();

                tvNode1.Tag = dr1[0];
                tvNode1.Text = Convert.ToString(dr1[1]).Trim(' ');
                tvNode1.ImageIndex = 0;
                tvNode1.SelectedImageIndex = 1;

                trv.Nodes.Add(tvNode1);


                foreach (DataRow dr2 in dt2.Rows)
                {
                    if (dr2[2] == dr1[0])
                    {
                        TreeNode tvNode2 = new TreeNode();
                        tvNode2.Tag = dr2[0];
                        tvNode2.Text = Convert.ToString(dr2[1]).Trim(' ');
                        tvNode2.ImageIndex = 2;
                        tvNode2.SelectedImageIndex = 3;
                        trv.Nodes[i].Nodes.Add(tvNode2);


                    }

                }
                i = i + 1;
            }

            trv.EndUpdate();
            trv.ExpandAll();

        }
    }
}
